import { Inject, Post, Body } from '@midwayjs/core';
import {
  CoolController, BaseController, TagTypes,
  CoolTag
} from '@cool-midway/core';
import { NursingPersonEntity } from '../../entity/person';
import { NursingPersonService } from '../../service/person';
import { BaseSysUserEntity } from '../../../base/entity/sys/user';
import { UserInfoEntity } from '../../../user/entity/info';
import { Context } from '@midwayjs/koa';

/**
 * 被护理人信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: NursingPersonEntity,
  service: NursingPersonService,
  // 向表插入当前登录用户ID
  insertParam: (ctx) => {
    console.log(ctx,'ctx')
    return {
      // 获得当前登录的后台用户ID，需要请求头传Authorization参数
      familyUserId: ctx.admin.userId,
    };
  },
  pageQueryOp: {
    keyWordLikeFields: ['a.name'],
    fieldEq: ['a.gender'],
    fieldLike: [],
    // fieldEqMany: [],
    select: ['a.*', 'c.nickName as familyUserName'],
    join: [
      // {
      //   entity: BaseSysUserEntity,
      //   alias: 'b',
      //   condition: 'a.createUserId = b.id',
      //   type: 'leftJoin',
      // },
      {
        entity: UserInfoEntity,
        alias: 'c',
        condition: 'a.familyUserId = c.id',
        type: 'leftJoin',
      },
    ],
    where: ctx => {
      const { startDate, endDate } = ctx.request.body;
      const condition = [];
      if (startDate && endDate) {
        condition.push([
          'a.birthday BETWEEN :startDate AND :endDate',
          { startDate, endDate },
        ]);
      }
      return condition;
    },
  },
})
export class AdminNursingPersonController extends BaseController {
  @Inject()
  nursingPersonService: NursingPersonService;
  
  @Inject()
  ctx;
}
